//
//  CCMineViewController.m
//  ZQReadProject
//
//  Created by 2.5 on 2021/9/7.
//

#import "CCMineViewController.h"
#import "CCMineHeaderTableViewCell.h"
#import "CCSettingTableViewCell.h"
#import "CLoginViewController.h"
#import "CCInfoViewController.h"
#import "CCSettingViewController.h"
#import "CCAboutViewController.h"
#import "HZStockMoreViewController.h"
@implementation CCMineViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self customeCCUI];
}
-(void)customeCCUI
{
    self.dataArray = [[NSMutableArray alloc] initWithArray:@[@{@"img":@"set11234",@"title":@"login in"},@{@"img":@"34",@"title":@"my information"},@{@"img":@"set366",@"title":@"setting"},@{@"img":@"set_defaultcc",@"title":@"about us"}]];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
    if(token&&token.length){
        self.dataArray = [[NSMutableArray alloc] initWithArray:@[@{@"img":@"set11234",@"title":NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"name"])},@{@"img":@"34",@"title":@"my information"},@{@"img":@"set366",@"title":@"setting"},@{@"img":@"set_defaultcc",@"title":@"about us"}]];
    }else{
        self.dataArray = [[NSMutableArray alloc] initWithArray:@[@{@"img":@"set11234",@"title":@"login in"},@{@"img":@"34",@"title":@"my information"},@{@"img":@"set366",@"title":@"setting"},@{@"img":@"set_defaultcc",@"title":@"about us"}]];
    }
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0){
        return 120;
    }else {
        return 80;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.dataArray.count>indexPath.row){
        if(indexPath.row ==0){
            CCMineHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CCMineHeaderTableViewCell"];
            if(!cell){
                cell = [[NSBundle mainBundle] loadNibNamed:@"CCMineHeaderTableViewCell" owner:self options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSDictionary *infoDic = self.dataArray[indexPath.row];
            cell.CCButton.userInteractionEnabled = NO;
//            cell.ccImageView.image = [UIImage imageNamed:infoDic[@"img"]];
            [cell.CCButton setTitle:infoDic[@"title"] forState:UIControlStateNormal];
            return cell;
        }else{
            CCSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CCSettingTableViewCell"];
            if(!cell){
                cell = [[NSBundle mainBundle] loadNibNamed:@"CCSettingTableViewCell" owner:self options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSDictionary *infoDic = self.dataArray[indexPath.row];
            cell.ccImageView.image = [UIImage imageNamed:infoDic[@"img"]];
            cell.ccLabel.text = infoDic[@"title"] ;
            return cell;
        }
        
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row ==0){
        NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
        if(!token||!token.length){
            CCLoginViewController *cvc = [[CCLoginViewController alloc] init];
            cvc.title = @"login";
            cvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cvc animated:YES];
        }
        
    }else if(indexPath.row ==1){
        NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
        if(token&&token.length){
            CCInfoViewController *cvc = [[CCInfoViewController alloc] init];
            cvc.title = @"info";
            cvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cvc animated:YES];
        }else{
            UIAlertController *avc =[UIAlertController alertControllerWithTitle:@"alert" message:@"Please login in!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil];
            [avc addAction:cancel];
            [self presentViewController:avc animated:YES completion:nil];
        }

    }
//    else if(indexPath.row ==2){
//        HZStockMoreViewController *pMoreVC = [HZStockMoreViewController new];
//        pMoreVC.parent = self;
//        pMoreVC.hidesBottomBarWhenPushed = YES;
//        [pMoreVC setViewData:@"market"];
//        [self.navigationController pushViewController:pMoreVC animated:YES];
//    }
    else if(indexPath.row ==2){
        CCSettingViewController *cvc = [[CCSettingViewController alloc] init];
        cvc.title = @"setting";
        cvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:cvc animated:YES];
    }else{
        CCAboutViewController *cvc = [[CCAboutViewController alloc] init];
        cvc.title = @"about us";
        cvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:cvc animated:YES];
    }
}
@end
